﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace CivilleWebRole
{
    public partial class ViewTasks : System.Web.UI.Page
    {
        string username;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/NotAuthorized.aspx");
            }

            username = User.Identity.Name;
        }

        protected void TasksGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            FileUpload fileUpload = TasksGridView.Rows[e.RowIndex].FindControl("LogoFileUpload") as FileUpload;
            if (fileUpload.HasFile)
            {
                String id = (Convert.ToInt32(getLastId()) + 1).ToString();
                fileUpload.SaveAs(System.IO.Path.Combine(Server.MapPath("Logo/Tasks"), id + System.IO.Path.GetExtension(fileUpload.FileName).ToLower()));
                CivilleAzureSqlDataSource.UpdateParameters["Logo"].DefaultValue = "~/Logo/Tasks/" + fileUpload.FileName;
            }
        }

        protected String getLastId()
        {
            String connectionString = ConfigurationManager.ConnectionStrings["CivilleConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand command = connection.CreateCommand();
            command.CommandText = "SELECT IDENT_CURRENT('Task')";
            connection.Open();
            String rsIdentity = command.ExecuteScalar().ToString();
            connection.Close();
            return rsIdentity;
        }
    }

}